package com.qf.service.impl;

import com.qf.dao.StudentRepository;
import com.qf.pojo.Student;
import com.qf.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Optional;

/**
 * @Description: 公用常量
 * @Data: 2022/9/14 10:14
 * @Pacakge: com.qf.service.impl
 * @ClassName: StudentServiceImpl
 * @Author: fan
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository studentRepository;
    /**
     * 查询所有学生信息
     * @return 学生列表
     */
    @Override
    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    /**
     * 修改与新增
     */
    @Override
    public void updateOrInsert(Student student) {
        studentRepository.saveAndFlush(student);
    }

    /**
     * 根据id删除某个学生
     * @param id
     */
    @Override
    public void deleteStudent(Integer id) {
        studentRepository.deleteById(id);
    }

    @Override
    public Student findById(Integer id) {
        Optional<Student> byId = studentRepository.findById(id);
        if (byId.isPresent()){
            return byId.get();
        }
        return null ;
    }
}
